package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import java.util.ArrayList;
import java.util.List;


import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractFormPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.WicketApplication;

/**
 * 
 * @author gpilla <gpilla@unq.edu.ar>
 *
 */
public class FolderFormPage extends AbstractFormPage{
	
	/**
	 * Este constructor se utiliza para recuperar desde la lista de carpetas.
	 * @param aModel
	 */
	public FolderFormPage (IModel aModel){
		super("Editar una carpeta", aModel);
		this.initializeForm(FolderListPage.class);
	}

	/**
	 * Este constructor se utiliza para la creación de nuevas carpetas
	 */
	public FolderFormPage() {
		super("Crear una nueva carpeta");
		Folder aFolder = new Folder("");
		this.isNew = true;
        this.setModel(new CompoundPropertyModel(aFolder));
        this.initializeForm(FolderListPage.class);
	}
	
	protected List<Folder> getParentFieldOptions() {
		
		Folder aFileSystem = ((WicketApplication) getApplication()).getModel().getFileSystem();
        
		List<Folder> options = new ArrayList<Folder>();
		options.add(aFileSystem);
		options.addAll(aFileSystem.getFullTreeAsList());
		
        return options;
	}

	@Override
	protected void addFields() {
		aForm.add(new FeedbackPanel("feedback"));
		aForm.add(new DropDownChoice("parent", new PropertyModel(getModelObject(), "parent"), getParentFieldOptions()).setRequired(true));
		aForm.add(new TextField("name").setRequired(true));
	}

	@Override
	protected void saveAction() {
		try {
			((WicketApplication) FolderFormPage.this.getApplication()).save();
			saveSucceed();
		} catch (Exception e) {
			warn(e.getMessage());
		}
		this.setResponsePage(FolderListPage.class);
	}

}
